<?php

class Request
{
	const ACTION_URL="%s/%s/";
	const PARAMETER_SEPARATOR="/";
	const PARAMETER_DELIMITER=":";
	const DEFAULT_MODULE="news";
	const DEFAULT_ACTION="index";
	const USE_MODREWRITE=false;
	const USE_ABSOLUTE_PATH=true;
	const SITE_URL="http://localhost/hacker/";
	private static $_requestModule;
	private static $_requestAction;
	private static $_parameters;
	private static $_post;
	private static $_error;
	private static function _parametersParse($string)
	{
		$parts=explode(self::PARAMETER_SEPARATOR, $string);
		$parameters=array();
		foreach($parts as $parameter)
		{
			if(!empty($parameter))
			{
				$s=explode(self::PARAMETER_DELIMITER, $parameter);
				if(count($s)==2)
				{
					$parameters[$s[0]]=$s[1];
				}
			}
		}
		return $parameters;
	}
	
	public static function init()
	{
		preg_match("/(.*?)\/(.*?)\/(.*)/", isset($_GET["q"])?$_GET["q"]:"", $request);
		self::$_requestModule=isset($request[1])?$request[1]:self::DEFAULT_MODULE;
		self::$_requestAction=isset($request[2])?self::decodeAction($request[2]):self::DEFAULT_ACTION;
		self::$_parameters=isset($request[3])?self::_parametersParse($request[3]):array();
		self::$_parameters=array_merge(self::$_parameters, $_POST);
		self::$_error=array();
	}
	
	public static function getRequestModule()
	{
		return self::$_requestModule;
	}
	
	public static function getRequestAction()
	{
		return self::$_requestAction;
	}
	
	public static function decodeAction($name)
	{
		$a=str_split($name);
		for($i=0;$i<count($a);$i++)
		{
			if($a[$i]=="-")
			{
				$a[$i]="";
				$a[$i+1]=strtoupper($a[$i+1]);
			}
		}
		return join("", $a);
	}
	
	public static function getURL($module, $action, $params=array())
	{
		$url=
			(self::USE_ABSOLUTE_PATH?self::SITE_URL:"").
			(self::USE_MODREWRITE?"":"index.php?q=").
			sprintf(self::ACTION_URL, $module, $action);
		$a=array();
		foreach($params as $k=>$v)
		{
			$a[]=$k.self::PARAMETER_DELIMITER.$v;
		}
		return $url.join(self::PARAMETER_SEPARATOR, $a);
	}
	
	public static function issetParameter($name)
	{
		return isset(self::$_parameters[$name]);
	}
	public static function getParameter($name)
	{
		if(!self::issetParameter($name))
		{
			throw new RequestException(sprintf("Nincs '%s' paraméter", $name));
		}
		return self::$_parameters[$name];
	}
	public static function setParameter($name, $value)
	{
		self::$_parameters[$name]=$value;
	}
	public static function getMethod()
	{
		return $_SERVER["REQUEST_METHOD"];
	}
	public static function isPost()
	{
		return self::getMethod() == "POST";
	}
	public static function hasError($param=false)
	{
		if(!$param)
		{
			return !empty(self::$_error);
		}
		return isset(self::$_error[$param]);
	}
	public static function getError($param)
	{
		if(!self::hasError($param))
		{
			throw new RequestException(sprintf("Nincs ilyen hiba (%s)", $param));
		}
		return self::$_error[$param];
	}
	public static function setError($param, $error)
	{
		self::$_error[$param]=$error;
	}
}